//
// findfile.cpp
//
// (C) Copyright 2000 Jan van den Baard.
//     All Rights Reserved.
//
#include "libutilsconfig.h"
#include "findfile.h"

LIB_NS_BEGIN

// Constructor. Initializes data.
TFileInfo::TFileInfo()
{
	// Initialize data.
	m_bFoundFile = FALSE;
	m_hHandle   = NULL;
	memset(( void * )&m_findData, 0, sizeof( WIN32_FIND_DATA ));
}

// Destructor. Closes the handle when it
// is still opened.
TFileInfo::~TFileInfo()
{
	// Close the handle is it is still
	// open.
	// Close the handle.
	if ( m_hHandle )
		::FindClose( m_hHandle );
}

// Get the length (only the lower DWORD).
DWORD TFileInfo::GetLength() const
{
	_ASSERT( m_bFoundFile );
	return m_findData.nFileSizeLow;
}

// Get the length.
DWORD TFileInfo::GetLength( DWORD& nFileSizeHigh ) const
{
	_ASSERT( m_bFoundFile );

	// Store the high DWORD and return the
	// low DWORD.
	nFileSizeHigh = m_findData.nFileSizeHigh;
	return m_findData.nFileSizeLow;
}

// Get the file name including extention.
BOOL TFileInfo::GetFileName( tstring& strName ) const
{
	_ASSERT( m_bFoundFile );

	try
	{
		// Find the last slash.
		LPCTSTR pszSlash = _tcsrchr( &m_findData.cFileName[ 0 ], _T( '\\' ));
		if ( pszSlash )
		{
			// Get a pointer to the next character which is
			// either a 0-character or the first character
			// of the file name.
			pszSlash = _tcsinc( pszSlash );

			// Set the string to the result.
			strName = pszSlash;
		}
		else
			// Use the whole name.
			strName = &m_findData.cFileName[ 0 ];
		return TRUE;
	}
	catch(...)
	{
		
	}
	return FALSE;
}

// Get the file path.
BOOL TFileInfo::GetFilePath( tstring& strName ) const
{
	_ASSERT( m_bFoundFile );

	try
	{
		// Set the base string.
		strName = m_strBase;

		// Add the file name.
		strName += m_findData.cFileName;
		return TRUE;
	}
	catch(...)
	{
		// Failure...
		
	}
	return FALSE;
}

// Get the file name excluding extention.
BOOL TFileInfo::GetFileTitle( tstring& strName ) const
{
	_ASSERT( m_bFoundFile );

	try
	{
		// First get the file name.
		if ( GetFileName( strName ))
		{
			// Find the offset of the extention.
			int nPos = strName.find( _T( '.' ));
			if ( nPos != -1 )
				// Truncate the name here.
				strName = strName.substr(0,nPos);
			return TRUE;
		}
	}
	catch(... )
	{
	}
	return FALSE;
}

// Get the file URL.
BOOL TFileInfo::GetFileURL( tstring& strName ) const
{
	_ASSERT( m_bFoundFile );

	try
	{
		// First we set the file:// string.
		strName = _T( "file://" );
		
		// Add the base/
		strName += m_strBase;

		// Than we add the path.
		strName += &m_findData.cFileName[ 0 ];
		return TRUE;
	}
	catch(...)
	{
		// Failure...
		
	}
	return FALSE;
}

// Get root directory of the file.
BOOL TFileInfo::GetRoot( tstring& strName ) const
{
	

	try
	{
		// Set the name to the base directory.
		strName = m_strBase;
		return TRUE;
	}
	catch(...  )
	{
		// 
	}
	return FALSE;
}

// See if the file name is "." or ".."
BOOL TFileInfo::IsDots() const
{
	

	// Get the file name.
	tstring str;
	if ( GetFileName( str ))
	{
		// Check the file name.
		if ( str ==  _T( "." )  || str ==  _T( ".." ))
			return 1;
		return 0;
	}
	// Failure...
	return -1;
}

// Close the handle.
void TFileInfo::Close()
{
	// Close the handle.
	if ( m_hHandle )
	{
		::FindClose( m_hHandle );
		m_hHandle = NULL;
	}

	// Reset data.
	m_strBase = _T("");
	m_bFoundFile = FALSE;
	memset(( void * )&m_findData, 0, sizeof( WIN32_FIND_DATA ));
}

// Open the handle.
BOOL TFileInfo::FindFile( LPCTSTR pszName )
{
	_ASSERT( m_hHandle == NULL || m_hHandle == INVALID_HANDLE_VALUE ); // Should not be is use!

	// Save the base.
	m_strBase = pszName;

	// Make sure we only have the directory.
	int nPos = m_strBase.rfind( _T( '\\' ));
	if ( nPos != -1 )
	{
		m_strBase =  m_strBase.substr(0, nPos + 1 );
	}
	
	// Open the handle.
	if (( m_hHandle = ::FindFirstFile( pszName, &m_findData )) != INVALID_HANDLE_VALUE )
		m_bFoundFile = TRUE;
	else
		m_bFoundFile = FALSE;

	return m_bFoundFile;
}

// Get the next name.
BOOL TFileInfo::FindNextFile()
{
	_ASSERT( m_bFoundFile );

	// Get the next file name.
	m_bFoundFile = ::FindNextFile( m_hHandle, &m_findData );
	return m_bFoundFile;
}

LIB_NS_END